# -*- coding: utf-8 -*-
import os
import sys
import time
import hashlib
from email import encoders
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
import smtplib


# 项目名称
project_name = "TraderMobileKYi"
# 项目的根目录
project_path = "/Users/zunxian/Desktop/k_yi/" + project_name
# 指定项目下编译目录
build_path = "build"
# 打包后ipa存储目录
targerIPA_parth = "/Users/zunxian/Desktop"
# 编译成功后.app所在目录
app_path = "/Users/zunxian/Desktop/k_yi/TraderMobileKYi/build/Build/Products/Release-iphoneos/%s.app" % project_name



# firm的api token
fir_install_path = "/Users/zunxian/.rvm/gems/ruby-2.0.0-p648/bin/fir"
fir_api_token = "f2385c044d2081f637c8e5b6b94f977a"
fir_api_downloadAddress = "https://fir.im/p6uf"


# 邮箱的信息，使用的smtp模式，所以邮箱的登录密码是 授权码登录密码，例如登录mac的系统邮箱一样
from_addr = "185XXXXXXXX@163.com"
password = "XXXXXXXX"
smtp_server = "smtp.163.com"
to_addr = 'XXXXXX@qq.com'


# clear 项目
def clear_project_build():
    os.system("cd "+project_path+";xcodebuild clean")
    print "clear project build success!"

# 创建build文件夹
def mkdir_project_build():
    os.system("cd "+project_path+";mkdir build")
    print "mkdir project build success!"

# 开始build项目
def build_project():
    print("build release start")
    os.system("xcodebuild -list")
    os.system('cd %s;xcodebuild -scheme %s -configuration release -derivedDataPath %s ONLY_ACTIVE_ARCH=NO || exit' % (project_path, project_name, build_path))
    # cocoapods项目使用下面一行代码
    # os.system('cd %s;xcodebuild -workspace %s.xcworkspace -scheme %s -configuration release -derivedDataPath %s ONLY_ACTIVE_ARCH=NO || exit' % (project_path, project_name, project_name, build_path))
    # CONFIGURATION_BUILD_DIR=./build/Release-iphoneos

# 打包ipa 并且保存在桌面
def build_ipa():
    global ipa_filename
    ipa_build_time = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time()))
    ipa_filename = '%s_%s.ipa' % (project_name,ipa_build_time)
    os.system ('xcrun -sdk iphoneos PackageApplication -v %s -o %s/%s'%(app_path,targerIPA_parth,ipa_filename))

#上传
def upload_fir():
    if os.path.exists("%s/%s" % (targerIPA_parth,ipa_filename)):
        print('watting...')
        # 直接使用fir 有问题 这里使用了绝对地址 在终端通过 which fir 获得，前提是你使用fir就需要先安装fir的命令行
        ret = os.system("%s p '%s/%s' -T '%s'" % (fir_install_path, targerIPA_parth,ipa_filename,fir_api_token))
    else:
        print("没有找到ipa文件")

def _format_addr(s):
    name, addr = parseaddr(s)
    return formataddr((Header(name, 'utf-8').encode(), addr))

# 发邮件
def send_mail():
    msg = MIMEText(('xx iOS测试项目已经打包完毕，请前往 %s 下载测试！') % fir_api_downloadAddress, 'plain', 'utf-8')
    msg['From'] = _format_addr('自动打包系统 <%s>' % from_addr)
    msg['To'] = _format_addr('xx测试人员 <%s>' % to_addr)
    msg['Subject'] = Header('xx iOS客户端打包程序', 'utf-8').encode()
    server = smtplib.SMTP(smtp_server, 25)
    server.set_debuglevel(1)
    server.login(from_addr, password)
    server.sendmail(from_addr, [to_addr], msg.as_string())
    server.quit()


clear_project_build()
mkdir_project_build()
build_project()
build_ipa()
upload_fir()
send_mail()